#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
template <typename T>
inline void read(T& x)
{
    int c=getchar(), f=1; x=0;
    while(c<'0'||'9'<c) {if(c=='-') f=-1; c=getchar();}
    while('0'<=c&&c<='9') 
        x=(x<<3)+(x<<1)+c-'0', c=getchar();
    x*=f;
}
template <typename T>
inline void quickwrite(T x)
{
    if(x>=10) quickwrite(x/10);
    putchar(x%10+'0');
}
#define DEBUG
using ll=long long;
ll n, daka, day, consday, ans, blank;

inline ll qmi(int base, int index)
{
    ll res=1;
    while(index)
    {
        if(index&1) res=(res*base);
        index>>=1;
        base=base*base;
    }
    return res;
}

void init()
{
    read(n);
}

void solve()
{
    init();
    while(++day<=n)
    {
        read(daka);
        if(daka)
        {
            
            if(blank) consday=max(consday-(1<<(blank-1)), 1ll*0);
            blank=0; ++consday; ans++;
            // if(consday<3) ans++;
            // else if(3<=consday&&consday<7) ans+=2;
            // else if(7<=consday&&consday<30) ans+=3;
            // else if(30<=consday&&consday<120) ans+=4;
            // else if(120<=consday&&consday<365) ans+=5;
            // else ans+=6;
            
            if(3<=consday) ans++;
            if(7<=consday) ans++;
            if(30<=consday) ans++;
            if(120<=consday) ans++;
            if(365<=consday) ans++;
        }
        else if(!daka) blank++;
        // printf("day:%d daka:%d consday:%d blank:%d ans:%d \n", day, daka, consday, blank, ans);
    }
    quickwrite(ans); puts("");
}

// #undef DEBUG
signed main()
{
    #ifdef DEBUG
        freopen("./in.txt", "r", stdin);
        freopen("./out.txt", "w", stdout);
    #endif

    int T=1; //scanf("%d", &T);
    while(T--) 
    {
        solve();
    }
    return 0;
}